package ru.ipo.dces.plugins;

import info.clearthought.layout.TableLayout;
import ru.ipo.dces.exceptions.GeneralRequestFailureException;
import ru.ipo.dces.pluginapi.Plugin;
import ru.ipo.dces.pluginapi.PluginEnvironment;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Илья
 * Date: 07.12.2008
 * Time: 20:08:15
 */
public class LineEnterPlugin extends JPanel implements Plugin {

    private JButton submitButton;
    private JTextField answerTextField;

    private final PluginEnvironment env;

    public LineEnterPlugin(PluginEnvironment pluginEnvironment) {
        env = pluginEnvironment;
        env.setTitle(env.getProblem().getName());

        setupUI();
        addListeners();
    }

    private void setupUI() {
        JComponent statementPanel = env.getStatementPanel();
        submitButton = new JButton("Отослать решение");
        answerTextField = new JTextField();
        JLabel label = new JLabel("Введите ответ");

        TableLayout layout = new TableLayout(
                new double[]{10, TableLayout.PREFERRED, 10, TableLayout.FILL, 10, TableLayout.PREFERRED, 10},
                new double[]{10, TableLayout.FILL, 10, TableLayout.PREFERRED, 10}
        );

        setLayout(layout);
        add(statementPanel, "1, 1 5, 1");
        add(label, "1, 3");
        add(answerTextField, "3, 3");
        add(submitButton, "5, 3");
    }

    private void addListeners() {
        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                /**TODO
                 * add a task solving status to this component:
                 * - If the task is solved, prohibit to send a solution to the server!!!
                 * - If not solved then permit sending a solution
                 *
                 * Represent a status (see SamplePluginProtocol.txt) with a label on the form (= status of a problem being solved)
                 * Statuses: not sent (can send), sent but wrong (can send), correct (can't send)
                 * If 'solved' from a server then green color and "Задача решена" message else red and other message
                 */

                if (answerTextField.getText().length() == 0) {
                    JOptionPane.showMessageDialog(null, "Вы не ввели ответ");
                    return;
                }
                if (JOptionPane.showConfirmDialog(null, "Подтвердите отсылку решения") == JOptionPane.YES_OPTION) {
                    HashMap<String, String> res = new HashMap<String, String>();
                    res.put("answer", answerTextField.getText());
                    try {
                        env.submitSolution(res);
                        JOptionPane.showMessageDialog(null, "Решение отослано!");
                        /*if (ans.get("result").equals("answer accepted"))
                            JOptionPane.showMessageDialog(null, "Вы дали правильный ответ!");
                        else if (ans.get("result").equals("wrong answer"))
                            JOptionPane.showMessageDialog(null, "Вы дали неправильный ответ!!");
                        else if (ans.get("result").equals("no submissions left"))
                            JOptionPane.showMessageDialog(null, "У Вас более не осталось попыток");
                        else
                            JOptionPane.showMessageDialog(null, "Ответ сервера: " + ans.get("result"));*/
                    } catch (GeneralRequestFailureException e1) {
                        JOptionPane.showMessageDialog(null, "Не удалось связаться с сервером. Ошибка: " + e1.getMessage());
                    }
                }
            }
        });
    }

    public JPanel getPanel() {
        return this;
    }

    public void activate() {
        //do nothing
    }

    public void deactivate() {
        //do nothing
    }
}